package com.mz.service;

import org.json.JSONObject;

import com.mz.base.util.ATTSystem;
import com.mz.base.util.DBUtil;
import com.mz.base.util.MD0Util;
import com.mz.entity.SysUsers;

public class LoginService {

	/**
	 * 
	 * @param request
	 * @return
	 */
	public JSONObject login(SysUsers users){
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		
		DBUtil db = new DBUtil();
		String sql = "select userId, userName, password, realName, roleId, status from sys_users " +
					 "where status<>"+ATTSystem.MARK_DELETE+" and userName='"+users.getUserName()+"'";
		JSONObject json = db.selectToJson(sql);
		
		if(json == null){
			retJson.put("msg", "账号不存在");
			return retJson;
		}
		if(json.getInt("status") != 1){
			retJson.put("msg", "该账号已经被禁用");
			return retJson;
		}
		if(!MD0Util.decryptionMD0ofStr(json.getString("password")).equals(users.getPassword())){
			retJson.put("msg", "账号与密码不匹配");
			return retJson;
		}
		
		users.setUserId(json.getLong("userId"));
		users.setUserName(json.getString("userName"));
		users.setPassword(json.getString("password"));
		users.setRealName(json.getString("realName"));
		users.setStatus(json.getInt("status"));
		users.setRoleId(json.getLong("roleId"));
		
		retJson.put("code", 1);
		retJson.put("msg", "成功");
		retJson.put("users", users);
		
		return retJson;
	}
	
	public static void main(String[] args) {
		System.out.println(MD0Util.encryptionMD0ofStr("123456"));
	}
	
}